#pragma once
#include <pthread.h>
#include <string>

typedef void (*callback_t)();

class Thread
{
public:
    static void *Routine(void *args)
    {
        Thread *th = static_cast<Thread*>(args);
        th->Entery();
    }

    Thread(callback_t func)
    :tid_(0), name_(""), start_time_(0), isrunning_(false), func_(func)
    {}

    void Run()
    {
        name_ = "Thread-"+std::to_string(num);
        start_time_ = time(nullptr);
        isrunning_ = true;
        pthread_create(&tid_, nullptr, Routine, this);
    }

    void Join()
    {
        pthread_join(tid_, nullptr);
        isrunning_ = false;
    }

    pthread_t get_tid()
    {
        return tid_;
    }

    std::string get_name()
    {
        return name_;
    }

    u_int64_t get_start_time()
    {
        return start_time_;
    }

    bool is_running()
    {
        return isrunning_;
    }

    void Entery()
    {
        func_();
    }

    ~Thread()
    {}
private:
    pthread_t tid_;
    std::string name_;
    u_int64_t start_time_; // 线程启动时间
    bool isrunning_;

    callback_t func_; // 线程将来要执行的函数, 回调函数
    static int num; // 线程编号
};

int Thread::num = 1;
